﻿namespace ExamV5.WorkExam
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class FrmJoinExamRubric : Page
    {
        protected Button butSave;
        protected GridView DataGridA;
        private double double_0 = 0.0;
        protected HtmlForm Form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        private HttpCookie httpCookie_4 = null;
        private int int_0 = 0;
        protected Label labATitle;
        protected Label labCheckRubricInfo;
        protected Label labRubricListAID;
        private PublicFun publicFun_0 = new PublicFun();
        protected HtmlInputHidden RndSaveTime;
        protected ScriptManager SM;
        private string string_0 = "";
        private string string_1 = "";
        protected string strTmpURL = "";
        protected HtmlInputHidden txtARubricListID;
        protected HtmlInputHidden txtCheckID;
        protected HtmlInputHidden txtHideComm;
        protected HtmlInputHidden txtHideEnter;
        protected HtmlInputHidden txtHideFocus;
        protected UpdatePanel UPPart;

        protected void butSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.DataGridA.Visible)
                {
                    this.method_5();
                }
                string strSql = "";
                if (!(this.publicFun_0.GetValues(string.Concat(new object[] { "select PaperID from WorkCheckPaper where UserCode='", this.httpCookie_0.Value, "' and PaperID=", this.int_0 }), "PaperID") == ""))
                {
                    strSql = string.Concat(new object[] { "update WorkCheckPaper set RubricSWrite='", this.string_1, "',RubricSFen='", this.string_0, "' where UserCode='", this.httpCookie_0.Value, "' and PaperID=", this.int_0 });
                }
                else
                {
                    strSql = "Insert into WorkCheckPaper(PaperID,UserCode,RubricSWrite,RubricSFen) values(" + this.int_0;
                    strSql = strSql + ",'" + this.httpCookie_0.Value.Trim() + "','" + this.string_1 + "','" + this.string_0 + "')";
                }
                this.publicFun_0.ExecuteSql(strSql);
            }
            catch
            {
            }
            finally
            {
                this.RndSaveTime.Value = "0";
                int num = new Random().Next(60);
                string connectionString = ConfigurationManager.ConnectionStrings["SaveMin"].ConnectionString;
                string str3 = ConfigurationManager.ConnectionStrings["SaveMax"].ConnectionString;
                if (num >= Convert.ToInt32(connectionString))
                {
                    if (num > Convert.ToInt32(str3))
                    {
                        this.RndSaveTime.Value = str3;
                    }
                    else
                    {
                        this.RndSaveTime.Value = num.ToString();
                    }
                }
                else
                {
                    this.RndSaveTime.Value = connectionString;
                }
                if (this.txtHideComm.Value.Trim() == "AutoSaveExam")
                {
                    this.labCheckRubricInfo.Text = "系统自动保存成功！<br><br>";
                }
                if (this.txtHideComm.Value.Trim() == "SaveExam")
                {
                    this.labCheckRubricInfo.Text = "人工保存成功！系统会自动保存,不用频繁操作！<br><br>";
                }
            }
        }

        private void method_0(string string_2)
        {
            string strSql = "";
            if (string_2 == "B")
            {
                string str2 = Convert.ToString(base.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
                if (!(!(str2 == "") ? (str2 != null) : false))
                {
                    str2 = Convert.ToString(base.Request.ServerVariables["REMOTE_ADDR"]);
                }
                string text1 = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();
                strSql = string.Concat(new object[] { "update WorkExamUser set LoginIP='", str2, "',Flag='", string_2, "' where UserCode='", this.httpCookie_0.Value.Trim(), "' and PaperID=", this.int_0 });
            }
            else
            {
                strSql = string.Concat(new object[] { "update WorkExamUser set LastDate='", DateTime.Now.ToShortDateString(), "',Flag='", string_2, "',GetCountFen='", this.double_0.ToString("0.00"), "' where UserCode='", this.httpCookie_0.Value.Trim(), "' and PaperID=", this.int_0 });
            }
            this.publicFun_0.ExecuteSql(strSql);
        }

        private void method_1()
        {
            string str2 = "";
            string str3 = "";
            string str4 = "";
            this.ViewState["ExamName"] = "";
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand(string.Concat(new object[] { "select b.SortInfo,a.ExamName,a.SubjectName,a.RubricDist,a.MemoDist,b.RubricS from WorkPaperInfo a ,WorkPaperData b where  b.UserCode='", this.httpCookie_0.Value, "' and a.ID=b.PaperID and a.ID=", this.int_0 }), connection);
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            if (!reader.Read())
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BNoData", "alert('对不起，没有读取到相关数据！');window.close();", true);
            }
            else
            {
                this.ViewState["ExamName"] = reader["ExamName"].ToString();
                str2 = reader["RubricDist"].ToString();
                str3 = reader["MemoDist"].ToString();
                str4 = reader["RubricS"].ToString();
                reader.Close();
                command.Dispose();
                connection.Close();
                connection.Dispose();
                string str5 = "";
                str5 = "<table   borderColor='#cccccc' cellSpacing='0' borderColorDark='#ffffff' cellPadding='0' width='100%' align='center' border='1' style='border-style:Solid;'>";
                str5 = str5 + "<tr height='30'><td align='left' bgcolor='#5AA6E4'>";
                this.labATitle.Visible = true;
                this.labATitle.Text = str5 + "<font color='#ffffff'><a name='RubricTypeA'></a>&nbsp;&nbsp;<b>一、测评题</b>&nbsp;&nbsp;(共<font color='#ff0000'>" + str2 + "</font>题,说明:" + str3 + ")</font></td></tr></table><br>";
                this.method_2(str4, "ID");
            }
        }

        private void method_2(string string_2, string string_3)
        {
            try
            {
                this.DataGridA.Visible = true;
                int num = 0;
                string selectCommandText = "Select ID,SubjectID,RubricType,RubricTitle,OptionAnswer,OKAnswer from WorkRubric where RubricType='A'";
                selectCommandText = selectCommandText + " and ID in(" + string_2.Replace("#", ",") + "0) order by " + string_3;
                SqlConnection selectConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, selectConnection);
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet, "SRubric");
                this.DataGridA.DataSource = dataSet.Tables["SRubric"].DefaultView;
                this.DataGridA.DataBind();
                new Random();
                for (int i = 0; i < this.DataGridA.Rows.Count; i++)
                {
                    string str3 = "";
                    num++;
                    if (i != 0)
                    {
                        this.labRubricListAID.Text = string.Concat(new object[] { this.labRubricListAID.Text, "<a href='#A", i.ToString(), "'><font color=blue>", num, "、</font></a>" });
                    }
                    else
                    {
                        this.labRubricListAID.Text = string.Concat(new object[] { "<br><B><a href='#RubricTypeA'>测评题</a>:</B><br><a href='#A", i.ToString(), "'><font color=blue>", num, "、</font></a>" });
                    }
                    string[] strArray2 = dataSet.Tables["SRubric"].Rows[i]["OptionAnswer"].ToString().Replace("####", "#").Replace("###", "#").Replace("##", "#").Split(new char[] { '#' });
                    Label label = (Label) this.DataGridA.Rows[i].FindControl("labADataTitle");
                    label.Text = string.Concat(new object[] { "<a title='标记此题'  href='#A", i.ToString(), "' onclick=javascript:SetTipRubricID('labRubricListAID','", num, "')><img src='../image/MenuICO/Menu36.gif' border=0></a>", num.ToString(), "、", label.Text.ToString(), "<a name='A", i.ToString(), "'></a>" });
                    RadioButtonList list = (RadioButtonList) this.DataGridA.Rows[i].FindControl("RBLAData");
                    list.Attributes.Add("onclick", "javascript:WriteRBL('labRubricListAID','" + num + "')");
                    for (int j = 0; j < strArray2.Length; j++)
                    {
                        if (!(strArray2[j].Trim() != ""))
                        {
                            continue;
                        }
                        switch (j)
                        {
                            case 0:
                                str3 = "A";
                                break;

                            case 1:
                                str3 = "B";
                                break;

                            case 2:
                                str3 = "C";
                                break;

                            case 3:
                                str3 = "D";
                                break;

                            case 4:
                                str3 = "E";
                                break;

                            case 5:
                                str3 = "F";
                                break;
                        }
                        ListItem item = new ListItem(strArray2[j].Replace("＃", "#"), str3);
                        list.Items.Add(item);
                    }
                    for (int k = 0; k < list.Items.Count; k++)
                    {
                        switch (k)
                        {
                            case 0:
                                list.Items[k].Text = "A、" + list.Items[k].Text;
                                break;

                            case 1:
                                list.Items[k].Text = "B、" + list.Items[k].Text;
                                break;

                            case 2:
                                list.Items[k].Text = "C、" + list.Items[k].Text;
                                break;

                            case 3:
                                list.Items[k].Text = "D、" + list.Items[k].Text;
                                break;

                            case 4:
                                list.Items[k].Text = "E、" + list.Items[k].Text;
                                break;

                            case 5:
                                list.Items[k].Text = "F、" + list.Items[k].Text;
                                break;
                        }
                    }
                }
                adapter.Dispose();
                dataSet.Clear();
                dataSet.Dispose();
                selectConnection.Dispose();
            }
            catch
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BRubricA", "alert('单选题显示出错,可能有错误格式的试题！')", true);
            }
        }

        private void method_3()
        {
            string str = "";
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand(string.Concat(new object[] { "select RubricSWrite from WorkCheckPaper where UserCode='", this.httpCookie_0.Value, "' and PaperID=", this.int_0 }), connection);
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            if (reader.Read())
            {
                str = reader["RubricSWrite"].ToString().Trim();
            }
            reader.Close();
            command.Dispose();
            connection.Close();
            connection.Dispose();
            if (str.Trim() != "")
            {
                this.method_4(str.Trim());
            }
        }

        private void method_4(string string_2)
        {
            int num = 0;
            string[] strArray = string_2.Split(new char[] { '|' });
            for (int i = 0; i < this.DataGridA.Rows.Count; i++)
            {
                num++;
                RadioButtonList list = (RadioButtonList) this.DataGridA.Rows[i].FindControl("RBLAData");
                if (strArray[i].Trim() != "未填写")
                {
                    list.Items.FindByValue(strArray[i].Trim()).Selected = true;
                    this.labRubricListAID.Text = this.labRubricListAID.Text.Replace("<font color=blue>" + num.ToString() + "、</font>", "<font color=green>" + num.ToString() + "、</font>");
                }
            }
        }

        private void method_5()
        {
            for (int i = 0; i < this.DataGridA.Rows.Count; i++)
            {
                try
                {
                    Label label = (Label) this.DataGridA.Rows[i].FindControl("labAOKAnswer");
                    string[] strArray = label.Text.Trim().Split(new char[] { '#' });
                    RadioButtonList list = (RadioButtonList) this.DataGridA.Rows[i].FindControl("RBLAData");
                    switch (list.SelectedIndex)
                    {
                        case 0:
                        {
                            this.string_1 = this.string_1 + "A|";
                            this.string_0 = this.string_0 + strArray[0].Trim() + "|";
                            this.double_0 += Convert.ToDouble(strArray[0].Trim());
                            continue;
                        }
                        case 1:
                        {
                            this.string_1 = this.string_1 + "B|";
                            this.string_0 = this.string_0 + strArray[1].Trim() + "|";
                            this.double_0 += Convert.ToDouble(strArray[1].Trim());
                            continue;
                        }
                        case 2:
                        {
                            this.string_1 = this.string_1 + "C|";
                            this.string_0 = this.string_0 + strArray[2].Trim() + "|";
                            this.double_0 += Convert.ToDouble(strArray[2].Trim());
                            continue;
                        }
                        case 3:
                        {
                            this.string_1 = this.string_1 + "D|";
                            this.string_0 = this.string_0 + strArray[3].Trim() + "|";
                            this.double_0 += Convert.ToDouble(strArray[3].Trim());
                            continue;
                        }
                        case 4:
                        {
                            this.string_1 = this.string_1 + "E|";
                            this.string_0 = this.string_0 + strArray[4].Trim() + "|";
                            this.double_0 += Convert.ToDouble(strArray[4].Trim());
                            continue;
                        }
                        case 5:
                        {
                            this.string_1 = this.string_1 + "F|";
                            this.string_0 = this.string_0 + strArray[5].Trim() + "|";
                            this.double_0 += Convert.ToDouble(strArray[5].Trim());
                            continue;
                        }
                    }
                    this.string_1 = this.string_1 + "未填写|";
                    this.string_0 = this.string_0 + "0|";
                }
                catch
                {
                    this.string_1 = this.string_1 + "未填写|";
                    this.string_0 = this.string_0 + "0|";
                    break;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            this.httpCookie_4 = new HttpCookie("ExamDeptName");
            this.httpCookie_4 = base.Request.Cookies["ExamDeptName"];
            if (!((this.httpCookie_0 != null) ? !(this.httpCookie_0.Value == "") : false))
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
            else
            {
                this.int_0 = Convert.ToInt32(base.Request["PaperID"].ToString());
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                this.httpCookie_4.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_4.Value));
                if (base.IsPostBack)
                {
                    this.labRubricListAID.Text = this.txtARubricListID.Value;
                    if (this.txtHideComm.Value.Trim() == "EndExam")
                    {
                        this.txtHideComm.Value = "";
                        string strSql = "";
                        try
                        {
                            if (this.DataGridA.Visible)
                            {
                                this.method_5();
                            }
                            if (this.publicFun_0.GetValues(string.Concat(new object[] { "select PaperID from WorkCheckPaper where UserCode='", this.httpCookie_0.Value, "' and PaperID=", this.int_0 }), "PaperID") == "")
                            {
                                strSql = "Insert into WorkCheckPaper(PaperID,UserCode,RubricSWrite,RubricSFen) values(" + this.int_0;
                                strSql = strSql + ",'" + this.httpCookie_0.Value.Trim() + "','" + this.string_1 + "','" + this.string_0 + "')";
                            }
                            else
                            {
                                strSql = string.Concat(new object[] { "update WorkCheckPaper set RubricSWrite='", this.string_1, "',RubricSFen='", this.string_0, "' where UserCode='", this.httpCookie_0.Value, "' and PaperID=", this.int_0 });
                            }
                            this.publicFun_0.ExecuteSql(strSql);
                            this.method_0("C");
                            base.Response.Write("<script>top.opener.location.href='FrmMyWorkExamList.aspx';top.self.close();</script>");
                        }
                        catch
                        {
                            this.method_0("G");
                            base.Response.Write("<script>top.opener.location.href='FrmMyWorkExamList.aspx';top.self.close();</script>");
                        }
                    }
                    if (this.txtHideComm.Value.Trim() == "CheckExam")
                    {
                        this.txtHideComm.Value = "";
                        this.labCheckRubricInfo.Text = "";
                        int num = 0;
                        string str = "0";
                        try
                        {
                            if (this.DataGridA.Visible)
                            {
                                num = 0;
                                while (num < this.DataGridA.Rows.Count)
                                {
                                    RadioButtonList list = (RadioButtonList) this.DataGridA.Rows[num].FindControl("RBLAData");
                                    if ((list.SelectedIndex == -1) && ((this.txtCheckID.Value != num.ToString()) && (Convert.ToInt32(this.txtCheckID.Value) < num)))
                                    {
                                        goto Label_0560;
                                    }
                                    num++;
                                }
                                if (num == this.DataGridA.Rows.Count)
                                {
                                    this.txtCheckID.Value = "-1";
                                }
                            }
                            if (this.labCheckRubricInfo.Text.Trim() == "")
                            {
                                this.labCheckRubricInfo.Text = "试卷答题全部完成！<br><br>";
                            }
                            return;
                        Label_0560:
                            str = Convert.ToString((int) (num + 1));
                            this.labCheckRubricInfo.Text = "测评题:第(" + str + ")题未完成！<br><br>";
                            this.txtCheckID.Value = num.ToString();
                        }
                        catch
                        {
                        }
                    }
                }
                else
                {
                    this.ViewState["ExamName"] = "";
                    this.ViewState["IEBack"] = "0";
                    if (this.publicFun_0.GetValues(string.Concat(new object[] { "select UserCode from WorkExamUser where Flag='C' and UserCode='", this.httpCookie_0.Value.Trim(), "' and PaperID=", this.int_0 }), "UserCode") == "")
                    {
                        this.method_1();
                        this.method_3();
                        string str5 = string.Concat(new object[] { "delete from Systip where GetNumber='", this.httpCookie_0.Value, "' and Formtable='WorkPaperInfo' and FormID=", this.int_0 });
                        str5 = str5 + ";insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('参加[" + this.ViewState["ExamName"].ToString() + "]测评考试并入考场！','" + this.httpCookie_1.Value + "','" + this.httpCookie_0.Value + "')";
                        this.publicFun_0.ExecuteSql(str5);
                        this.method_0("B");
                        this.RndSaveTime.Value = "0";
                        int num2 = new Random().Next(60);
                        string connectionString = ConfigurationManager.ConnectionStrings["SaveMin"].ConnectionString;
                        string str3 = ConfigurationManager.ConnectionStrings["SaveMax"].ConnectionString;
                        if (num2 >= Convert.ToInt32(connectionString))
                        {
                            if (num2 > Convert.ToInt32(str3))
                            {
                                this.RndSaveTime.Value = str3;
                            }
                            else
                            {
                                this.RndSaveTime.Value = num2.ToString();
                            }
                        }
                        else
                        {
                            this.RndSaveTime.Value = connectionString;
                        }
                        ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BTip", "alert('退出考场时请单击[交卷]按钮，否则以零分计算！')", true);
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('对不起，此份测评试卷您已经交卷，无法再操作！')", true);
                    }
                }
            }
        }
    }
}

